package com.app.web.sys.pic;

import com.alibaba.fastjson.JSON;
import com.app.utils.base.http.ResultUtil;
import com.app.utils.base.j2se.Common;
import com.app.utils.base.j2se.Convert;
import com.app.utils.base.j2se.Logger;
import com.app.utils.base.j2se.OssFileUtil;
import com.app.web.user.app.AppInfo;
import com.app.web.user.app.AppInfoManager;
import com.app.web.user.info.UserInfo;
import com.app.web.user.info.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Description
 * Created by 王伟
 * Date create on 10:00 2019/10/29.
 */
@RestController
@RequestMapping("/system/pic/")
public class PicController {
    @Autowired
    private UserInfoService userInfoModel;

    // 生成 邀请图片
    @RequestMapping("generateUserImg")
    public String generateUserImg(@RequestAttribute int uid, String sharePage, String appid) {
        try {
            sharePage = sharePage.replaceAll("register","login");
            String ossPath = "ym/user/tbyy/share/qrcode/";
            Map<String,Object> paramMap = new HashMap<>();
            paramMap.put("uid",uid);
            paramMap.put("page",sharePage);
            paramMap.put("appId",appid);
            String imgName = userInfoModel.getUserShareImg(paramMap);
            if(Common.isEmpty(imgName)){
                //app判断
                AppInfo appInfo = AppInfoManager.get(appid);
                if(null == appInfo){
                    return ResultUtil.error("8002", "参数异常");
                }
                // 小程序二维码
                UserInfo userInfo = userInfoModel.info(uid);
                File file = appInfo.getWxMaService().getQrcodeService().createWxaCodeUnlimit(Convert.objToStr(userInfo.getMcode()), sharePage, 800, false, null, false);
                String fileName = file.getName();
                //获取图片后缀名
                String picType = fileName.substring(fileName.lastIndexOf(".", fileName.length()));
                //设置图片名称
                imgName = UUID.randomUUID().toString() + picType;
                //上传到OSS
                OssFileUtil.putFile(ossPath + imgName, file);
                paramMap.put("imgName", imgName);
                userInfoModel.saveUserShareImg(paramMap);
            }
            return ResultUtil.success(OssFileUtil.DOMAIN + ossPath + imgName);
        }catch (Exception e){
            e.printStackTrace();
            Logger.error("生成分享邀请小程序码失败",e);
            return ResultUtil.error(6001,"生成分享邀请小程序码失败");
        }
    }

    /**
     * 通用小程序码生成
     * @param qrcode
     * @return
     */
    @RequestMapping("/generateQrcode")
//    @RedisCache
    public String generateQrcode(String qrcode) {
        try {
            Map<String, Object> param = JSON.parseObject(qrcode).getInnerMap();
            String sharePage = Convert.objToStr(param.get("sharePage"));
            String name = Convert.objToStr(param.get("name"));
            String val = Convert.objToStr(param.get("val"));
            String appid = Convert.objToStr(param.get("appid"));

            String ossPath = "back/universal/qrcode/tbyy/";
            String[] pages = sharePage.split("/");
            ossPath = ossPath + pages[pages.length-2] + "/";

            //app判断
            AppInfo appInfo = AppInfoManager.get(appid);
            if(null == appInfo){
                return ResultUtil.error("8002", "参数异常");
            }
            // 小程序二维码
            File file = appInfo.getWxMaService().getQrcodeService().createWxaCodeUnlimit(val, sharePage, 300, false, null,false);
            String fileName = file.getName();
            //获取图片后缀名
            String picType = fileName.substring(fileName.lastIndexOf(".", fileName.length()));
            //设置图片名称
            String imgName = val + picType;
            //上传到OSS
            OssFileUtil.putFile(ossPath + imgName, file, 60*10);
            return ResultUtil.success(OssFileUtil.DOMAIN + ossPath + imgName);
        }catch (Exception e){
            Logger.error("通用小程序码生成失败",e);
            return ResultUtil.error(6001,"生成分享邀请小程序码失败");
        }
    }
}
